<?php

namespace Modules\Car\Http\Controllers;

use App\Helpers\ResponseData;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Modules\Car\Entities\CarOrder;
use Modules\Car\Entities\Customer;
//顾客管理
class CustomController extends Controller
{
    use ResponseData;
    /**
     * 用户搜索
     * @param Request $request
     * @return mixed
     */
    public function searchCustomer(Request $request){
        $size = $request->input('size',10);
        $true_name = $request->input('truename','');
        $username = $request->input('username','');
        $phone = $request->input('phone','');

        $data = User::query()
            ->with('customer')
            ->when($true_name,function($query)use($true_name){
                $query->where('truename','like','%'.$true_name.'%');
            })
            ->when($username,function($query)use($username){
                $query->where('username','like','%'.$username.'%');
            })

            ->when($phone,function($query)use($phone){
                $query->where('phone',$phone);
            })
            ->where('role',0)
            ->where('department',0)
            ->whereNull('deleted_at')
            ->paginate($size);
        return $this->success($data);
    }
    public function list(Customer $customer,Request $request){
        $search = $request->input('search','');
        // $user = $request->user();
        $model = $customer->query()
            ->with('foot')->withCount(['orders'=>function($query)use($request){
                $query->where('status',6)->whereIn('process_status',[4,5,6]);
            }]);
        if($search){
            $model = $model
                ->where(function($query)use($search){
                    $query->orWhere('id_number','like','%'.$search.'%')
                        ->orWhere('name','like','%'.$search.'%')
                        ->orWhere('phone','like','%'.$search.'%');
                });
        }
        if($request->filled('is_black')){
            $model = $model->where('is_black',0);
        }


        // if($user->role&&$user->role!=1){
        //     if(!$user->department){
        //         return $this->failed('非法用户',201);
        //     }else{
        //         $model = $model->where('department_id',$user->department);
        //     }

        // }
        if($request->filled('department_id')){
            $department_id = explode(",",$request->department_id);
            $model = $model->whereIn('department_id',$department_id);
        }
        if($request->filled('source')){
            $source = explode(",",$request->source);
            $model = $model->whereIn('source',$source);
        }
//        if($user->post_id){
//            $model = $model->where('department_id',$user->department_id);
//        }else{
//
//        }

        $model->whereNull('deleted_at')->orderBy('id','desc');
        if($request->filled('size')){
            $data =  $model->paginate($request->size);
        }else{
            $data = $model->get();
        }
        return $this->success($data);
    }
    //保存小程序接口
    public function store(Customer $customer,Request $request){
        if($request->filled('id_number')){
            $is_user = $customer->query()
                ->with('departments')
                ->where('id_number',$request->id_number)->first();
            if($is_user){
               // info(json_encode($is_user));
                $manager = $is_user->departments->name;

                return $this->failed("此用户已经被{$manager}店长添加,请勿重复添加",201);
            }
        }
        $user = $request->user();
        $data = $request->except('id');
        if($user->department_id){
            $data['department_id'] = $user->department_id;
        }
        $customer->fill($data);
        $customer->save();

        return $this->success($customer);
    }
    public function edit(Customer $customer,Request $request){
        $data = $customer->find($request->id);
        $totalsum = CarOrder::query()->where('customer_id',$request->id)->where('status',6)->whereIn('process_status',[4,6])->selectRaw('count(id) as counttotal,sum(fact_pay_total) as mount_all')->first();
        if($data){
            $data['lease_times'] = $totalsum->counttotal;
            // $data['mount_all'] = $totalsum->mount_all; // 改为存数据库了，不实时统计
        }
        return $this->success($data);
    }
    public function update(Customer $customer,Request $request){
        $data = $customer->find($request->id);
        if($request->filled('id_number')){
            $is_user = $customer->query()
                ->with('departments')
                ->where('id','!=', $request->id)
                ->where('id_number',$request->id_number)->first();
            if($is_user){
               // info(json_encode($is_user));
                $manager = $is_user->departments->name;

                return $this->failed("此用户已经被{$manager}店长添加,请勿重复添加",201);
            }
        }
        if($data){
            $data->update($request->all());
            // 更新订单里的全部客户信息
            CarOrder::query()->where('customer_id',$request->id)->update(['wx_name'=>$request->name]);
            return $this->success('修改成功');
        }else{
            return $this->failed('用户不存在',201);
        }

    }
    // 更新顾客租金总金额
    public function update_amount(Customer $customer,Request $request){
        $data = $customer->find($request->id);
        if($data){
            $mount_all = $data->mount_all;
            $data->mount_all = round($mount_all+$request->amount,2);
            $data->save();
            return $this->success('客户租车总金额更新成功');
        }else{
            return $this->failed('用户不存在',201);
        }

    }
    // 更新顾客首租信息
    public function update_hire(Customer $customer,Request $request){
        $data = $customer->find($request->id);
        if($data&&$request->filled('vehicle_type')){
            // vehicle_type 首租车型
            // lease_days 首租天数
            // is_deposit_free 是否免押金，1不免押 2免押
            if(empty($data->vehicle_type)){
                $data->vehicle_type = $request->vehicle_type;
                $data->lease_days = $request->lease_days;
                $data->is_deposit_free = $request->is_deposit_free;
                $data->save();
                return $this->success('客户首租信息更新成功');
            }else{
                return $this->success('客户首租信息已存在');
            }
        }else{
            return $this->failed('用户不存在或首租信息不存在',201);
        }

    }
    public function destory(Customer $customer,Request $request){
        $data = $customer->find($request->id);
        $data->deleted_at = now();
        $data->save();
        return $this->success('删除成功');
    }
}
